<template>
	<view class="evan-form-show">
		<l-file ref="lFile" @up-success="onSuccess"></l-file>
		<evan-form :hideRequiredAsterisk="hideRequiredAsterisk" ref="form">
			<evan-form-item label="昵称：">
				<input class="form-input" v-model="coachNickname" placeholder-class="form-input-placeholder" placeholder="请输入昵称" />
			</evan-form-item>
			<evan-form-item label="密码修改">
			</evan-form-item>
			<evan-form-item label="原密码：">
				<input class="form-input" password="true" v-model="coachOldPassword" placeholder-class="form-input-placeholder"
				 placeholder="请输入原密码" />
			</evan-form-item>
			<evan-form-item label="新密码：">
				<input class="form-input" password="true" v-model="coachNewPassword" placeholder-class="form-input-placeholder"
				 placeholder="请输入新密码" />
			</evan-form-item>
			<evan-form-item label="确认新密码：">
				<input class="form-input" password="true" v-model="coachReNewPassword" placeholder-class="form-input-placeholder"
				 placeholder="请再次输入" />
			</evan-form-item>
			<evan-form-item label="个人头像：">
			</evan-form-item>
			<image style="width:180upx ;height: 180upx;" :src="coach.coachHeadPortrait"></image>
			<evan-form-item label="修改头像：" prop="coachHeight">
			</evan-form-item>
			<uImg ref="upimg" :canUploadFile="true" :limit="limitNum" :uploadFileUrl="uploadFileUrl" :heaer="header"
			 :fileKeyName="name" :uImgList.sync="uImgList" @uploadSuccess="uploadSuccess"></uImg>
		</evan-form>
		<button @click="next" class="evan-form-show__button">确定</button>
	</view>
</template>

<script>
	import EvanForm from '@/components/evan-form/evan-form.vue'
	import EvanFormItem from '@/components/evan-form/evan-form-item.vue'
	import uImg from '@/components/zhtx-uploadImg/zhtx-uploadImg.vue'
	import base from '@/common/base.js'
	let toast = msg => {
		uni.showToast({
			title: msg,
			icon: 'none',
		});
	}
	export default {
		components: {
			EvanForm,
			EvanFormItem,
			uImg
		},
		data() {
			return {
				msg: '',
				limitNum: 1,
				uploadFileUrl: base.urlPrefix + 'yoga_app/coach-info/coachUploadPersonPortrait',
				name: 'file', //上传的名字
				header: { // 如果需要header，请上传
				},
				uImgList: '',
				length: 140,
				hideRequiredAsterisk: false,
				index: 0,
				coachNickname: '',
				coachOldPassword: '',
				coachNewPassword: '',
				coachReNewPassword: '',
				coach: []
			}
		},
		onLoad() {
			// 这里必须放在$ne，不然h5会找不到this.$refs.form
			this.$nextTick(() => {
				this.$refs.form.setRules(this.rules)
			});
			this.getCoachInfo();
		},
		methods: {
			uploadSuccess(result) {
				if (result.statusCode == 200) {
					//上传成功的回调处理
					base.coach.coachHeadPortrait = result.data;
					this.getCoachInfo();
				} else {
					toast('上传失败')
				}
			},
			upFile() {
				this.$refs.upimg.uploadCoachHeadPortraitImg();
			},
			next() {
				if (this.coachOldPassword.length > 0) {
					uni.request({
						url: base.urlPrefix + "yoga_app/coach-info/coachJudgeOldPassword",
						data: {
							coachInputPassword: this.coachOldPassword,
							coachId: base.coach.coachId
						},
						success: (res) => {
							if (res.data.code == "error") {
								toast("原密码错误");
							} else {
								if (this.coachNewPassword.length < 6) {
									toast("密码长度不能小于6位");
								} else {
									if (this.coachNewPassword != this.coachReNewPassword) {
										toast("两次输入的密码不一致");
									} else {
										if (this.coachNickname.length == 0) {
											toast("昵称不能为空");
										} else {
											if (this.uImgList.length > 0) {
												this.upFile();
											}
											uni.request({
												url: base.urlPrefix + 'yoga_app/coach-info/coachReviewPersonMessage',
												data: {
													coachId: base.coach.coachId,
													newPassword: this.coachNewPassword,
													coachNickname: this.coachNickname
												},
												success: (res) => {
													toast("修改成功");
													base.coach.coachNickname=this.coachNickname
													uni.switchTab({
														url: "/pages/tabBar/user/user",
														success() {
															let page = getCurrentPages().pop();
															page.onLoad();
														}
													});
												}
											});
										}
									}
								}
							}
						}
					});
				} else {
					if (this.coach.coachNickname != this.coachNickname) {
						if(this.coachNewPassword.length==0&&this.coachReNewPassword.length==0){
							if (this.coachNickname.length != 0) {
								if (this.uImgList.length > 0) {
									this.upFile();
								}
								uni.request({
									url: base.urlPrefix + 'yoga_app/coach-info/coachReviewPersonMessage',
									data: {
										coachId: base.coach.coachId,
										newPassword: this.coachNewPassword,
										coachNickname: this.coachNickname
									},
									success: (res) => {
										toast("修改成功");
										base.coach.coachNickname=this.coachNickname;
										uni.switchTab({
											url: "/pages/tabBar/user/user",
										});
									}
								});
							} else {
								toast("昵称不能为空");
							}
						}else{
							toast("密码修改失败");
						}
					} else {
						if (this.coachNewPassword.length > 0 || this.coachReNewPassword.length > 0) {
							toast("密码修改失败");
						} else {
							if (this.uImgList.length > 0) {
								this.upFile();
								toast("头像修改成功");
							}
						}
						uni.switchTab({
							url: "/pages/tabBar/user/user",
						});
					}

				}
			},
			hideReqired() {
				this.hideRequiredAsterisk = !this.hideRequiredAsterisk
			},
			getCoachInfo() {
				uni.request({
					url: base.urlPrefix + 'yoga_app/coach-info/getCoachInfoById',
					data: {
						coachId: base.coach.coachId
					},
					success: (res) => {
						this.coach = res.data.object;
						this.coachNickname = this.coach.coachNickname;
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.form-picker-placeholder {
		margin-right: 5rpx;
	}

	.evan-form-show {
		padding: 20rpx 30rpx;

		.form-label {
			margin-top: 30rpx;
		}

		.form-input {
			font-size: 28rpx;
			color: #333;
			text-align: right;
		}

		.form-input-placeholder {
			font-size: 28rpx;
			color: #999
		}

		&__button {
			width: 100%;
			height: 88rpx;
			border-radius: 8rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 0;
			font-size: 36rpx;
			color: #fff;
			margin-top: 20rpx;
			background-color: #2D87D5;

			&::before,
			&::after {
				border: none;
			}
		}
	}
</style>
